# -*- coding:utf-8 *-
# 项目       :    练习题 
# 文件       :    Collects.py
# 作者       :    zhangchen 
# 时间       :    2021/4/23  3:51 下午 
# IDE       :    PyCharm
# from 测试平台汇总数据.Sum_collect_xin import sums
import datetime


class summary:
    def date_format_ymd(self, date, format_type, day):
        """
        操作时间并进行格式化
        :param date: 指定时间
        :param format_type: 格式化类型
        :param day: 操作加减时间：hours=1
        :return: 格式化后时间
        """
        return (datetime.datetime.strptime(date, '%Y-%m-%d') + datetime.timedelta(days=day)).__format__(
            format_type)

    def cal_whole_project_nums(self, n, r):
        rate = r / n
        return float('%.4f' % rate)

    def sum_inflow_demand(self, data):
        """
        参与项目数量_流入需求
        """
        ymd = datetime.datetime.now().strftime('%Y-%m-%d')
        data_start_f = self.date_format_ymd(ymd, '%Y-%m-%d %H:%M:%S', -6)
        last_week_start = datetime.datetime.strptime(data_start_f, "%Y-%m-%d %H:%M:%S").date()
        last_week_end = datetime.datetime.strptime(ymd + ' 23:59:59', "%Y-%m-%d %H:%M:%S").date()
        count = len([1 for i in data.begindate if i >= last_week_start and i <= last_week_end])
        return count

    def sum_online_demand(self, data):
        """
        参与项目数量_上线需求
        """
        ymd = datetime.datetime.now().strftime('%Y-%m-%d')
        data_start_f = self.date_format_ymd(ymd, '%Y-%m-%d %H:%M:%S', -6)
        last_week_start = datetime.datetime.strptime(data_start_f, "%Y-%m-%d %H:%M:%S").date()
        last_week_end = datetime.datetime.strptime(ymd + ' 23:59:59', "%Y-%m-%d %H:%M:%S").date()
        count = len([1 for i in data.onlinedate if i and i >= last_week_start and i <= last_week_end])
        return count

    def sum_complete_demand(self, data):
        """
        参与项目数量_完成需求
        """
        ymd = datetime.datetime.now().strftime('%Y-%m-%d')
        data_start_f = self.date_format_ymd(ymd, '%Y-%m-%d %H:%M:%S', -6)
        last_week_start = datetime.datetime.strptime(data_start_f, "%Y-%m-%d %H:%M:%S").date()
        last_week_end = datetime.datetime.strptime(ymd + ' 23:59:59', "%Y-%m-%d %H:%M:%S").date()
        count = len([1 for i in data.enddate if i >= last_week_start and i <= last_week_end])
        return count

    def sum_project_type_标准需求(self, data):
        """
        参与项目数量_标准需求
        """
        count = len([1 for i in data.ptype if i == "1"])
        return count

    def sum_project_type_验收需求(self, data):
        """
        参与项目数量_验收需求
        """
        count = len([1 for i in data.ptype if i == "2"])
        return count

    def sum_project_type_线上bug(self, data):
        """
        参与项目数量_线上bug
        """
        count = len([1 for i in data.ptype if i == "3"])
        return count

    def sum_project_type_生产事故(self, data):
        """
        参与项目数量_生产事故
        """
        count = len([1 for i in data.ptype if i == "4"])
        return count

    def sum_project_type_紧急发版(self, data):
        """
        参与项目数量_紧急发版
        """
        count = len([1 for i in data.ptype if i == "5"])
        return count

    def sum_project_nums(self, data):
        """
        参与项目数量
        """
        sum_project_nums = len(data)
        return sum_project_nums

    def sum_count_times(self, data):
        """
        总工时
        """
        sum_count_times = sum(data.spendtime)
        return sum_count_times

    def sum_access_rate(self, data):
        '''
        准入率计算
        :param group: 业务线
        :param zhunrucount:准入次数
        :param ticecount: 提测次数
        :return: 准入率
        '''
        sum_access_rates = sum(data.zhunrucount) / sum(data.ticecount)
        sum_access_rate = format(sum_access_rates, ".4")
        return sum_access_rate

    def sum_must_rate(self, data):
        '''
        准出率计算
        :param group: 业务线
        :param zhunchucount:准出次数
        :param ticecount: 提测次数
        :return: 准出率
        '''
        sum_must_rates = sum(data.zhunchucount) / sum(data.ticecount)
        sum_must_rate = format(sum_must_rates, ".4")
        return sum_must_rate

    def sum_access_on_rate(self, data):
        '''
        按时准入率
        :param group: 业务线
        :param anticecount: 按时提测次数
        :return: 按时准入率
        '''
        ticecount = list(filter(None, data.anticecount))
        access_on = self.sum_project_nums(data)
        access = sum(ticecount)
        access_on_rate = access / access_on
        sum_access_on_rate = format(access_on_rate, ".4")
        return sum_access_on_rate

    def sum_must_out_rate(self, data):
        """
        按时准出率
        :param group: 业务线
        :param anzhuchucount: 按时准入次数
        :return:
        """
        anzhuchucount = list(data.anzhuchucount)
        out_rate = len(anzhuchucount)
        sum_must = sum(anzhuchucount)
        sum_must_out_rate = format(sum_must / out_rate, ".4")

        return sum_must_out_rate

    def sum_release_success(self, data):
        """
        发布准时率
        :param group: 业务线
        :param isonline: 是否按时上线次数
        :return:
        """
        isonline = list(data.isonline)
        release_success = self.sum_project_nums(data)
        release_success1 = sum(isonline)
        sum_release_success = format(release_success1 / release_success, ".4")
        return sum_release_success

    def sum_repair_rate(self, data):
        """
        bug修复率
        :param group: 业务线
        :param yiliubug: 遗留bug数
        :param p1: p1数量
        :param p2: p2数量
        :param p3: p3数量
        :param p4: p4数量
        :param p5: p5数量
        :param tiyanbug: 提测次数
        :param weibugcount: 当前版本未解决bug总数
        :return:
        """
        P1 = data.P1
        P2 = data.P2
        P3 = data.P3
        P4 = data.P4
        P5 = data.P5
        try:
            repair_rate = (sum(data.yiliubug + P1 + P2 + P3 + P4 + P5 + data.tiyanbug) - sum(data.weibugcount)) \
                          / sum(data.yiliubug + P1 + P2 + P3 + P4 + P5 + data.tiyanbug)
            sum_repair_rate = format(repair_rate, ".4")
        except ZeroDivisionError:
            sum_repair_rate = 0
        return sum_repair_rate

    def sum_bug_rate(self, data):
        """
        bug率
        :param group: 业务线
        :param p1: p1数量
        :param p2: p2数量
        :param p3: p3数量
        :param p4: p4数量
        :param spendtime: 研发工时
        :param leve: bug级别系数
        :return:
        """
        leve = [3, 2, 1, 0.5]
        P1 = data.P1
        P2 = data.P2
        P3 = data.P3
        P4 = data.P4
        bug_rate = sum(P1) * leve[0] + sum(P2) * leve[1] + sum(P3) * leve[2] + sum(P4) * leve[3]

        try:
            bug_rates = bug_rate / sum(data.spendtime)
            sum_bug_rate = round(bug_rates, 4)
        except ZeroDivisionError:  # 'ZeroDivisionError'除数等于0的报错方式
            sum_bug_rate = 0.0

        return sum_bug_rate

    def sum_invalid_rate(self, data):
        """
        无效修改率
        :param group: 业务线
        :param reopenbug: reopen数量
        :param yiliubug: 遗留bug总数
        :param tiyanbug: 体验bug数量
        :param p1: p1数量
        :param p2: p2数量
        :param p3: p3数量
        :param p4: p4数量
        :param p5: p5数量
        :return:
        """
        P1 = data.P1
        P2 = data.P2
        P3 = data.P3
        P4 = data.P4
        P5 = data.P5
        try:
            invalid_rate = sum(data.reopenbug) / sum(data.yiliubug + data.tiyanbug + P1 + P2 + P3 + P4 + P5)
            sum_invalid_rate = round(invalid_rate, 4)
        except ZeroDivisionError:
            sum_invalid_rate = 0
        return sum_invalid_rate

    def sum_release_rate(self, data):
        """
        发布成功率
        :param group: 业务线
        :param onlinecount: 上线次数
        :param rollcount: 回滚次数
        :return:
        """
        release_rate = sum(data.onlinecount) - sum(data.rollcount)
        release_rates = release_rate / sum(data.onlinecount)
        sum_release_rate = format(release_rates, ".4")
        return sum_release_rate

    def sum_change(self, data):
        """
        需求及变更次数
        :param group: 业务线
        :param dmchange: 需求变更次数
        :param dmcount: 需求类bug统计
        :return:
        """
        sum_change = sum(data.dmchange + data.dmcount)
        return sum_change

    def updated_time(self):
        """
        获取当前时间
        :return:
        """
        updated_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        return updated_time
